package com.xiaoma.controller;

import com.alipay.api.AlipayApiException;
import com.xiaoma.config.AlipayTemplate;
import com.xiaoma.entity.Category;
import com.xiaoma.entity.PayVo;
import com.xiaoma.feign.EsFeignService;
import com.xiaoma.service.CartService;
import com.xiaoma.service.CategoryService;
import com.xiaoma.service.GoodsService;
import entity.Goods;
import entity.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.JwtUtils;
import utils.R;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @Author: xiaoma
 * @Date: 2021/7/8 14:31
 * @description:
 */
@RestController
@RequestMapping("/goods")
@Slf4j
public class GoodsController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    EsFeignService esFeignService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AlipayTemplate alipayTemplate;
    @Autowired
    private CartService cartService;

    @GetMapping("/queryAllCategory")
    public R listCategory(HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        log.info(id);
        List<Category> list = categoryService.queryAll();
        return R.ok().put("list", list);
    }

    @GetMapping("/queryAll")
    public R list() {
        List<Goods> list = goodsService.listGoods();
        return R.ok().put("list", list);
    }

    @GetMapping("/queryGoodsById/{id}")
    public Goods queryGoodsById(@PathVariable("id") Integer id) {
        return goodsService.getById(id);
    }

    @PostMapping("/product")
    public R productGoods(@RequestBody Goods goods) {
        return esFeignService.productGoods(goods);
    }

    @GetMapping("/queryProductGoodsInEs/{keyword}")
    R queryProductGoodsInEs(@PathVariable String keyword) {
        return esFeignService.queryProductGoodsInEs(keyword);
    }

    @PostMapping("/queryById")
    public R queryById(@RequestBody String id, HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        GoodsVo goodsVo = goodsService.queryGoodsVoById(id, uid);
        return R.ok().put("data", goodsVo);
    }

    @GetMapping(value = "/payOrder")
    public String payOrder() throws AlipayApiException {
        return alipayTemplate.pay(new PayVo(UUID.randomUUID().toString(), "订单1", "12.00", "商品描述"));
    }

    @GetMapping(value = "/lockStock/{id}/{num}")
    public Integer lockStock(@PathVariable String id, @PathVariable Integer num) {
        return goodsService.lockStock(id, num);
    }

    @GetMapping(value = "/incrementLike/{gid}")
    public Long incrementLike(@PathVariable String gid) {
        return goodsService.incrementLike(gid);
    }

    @GetMapping(value = "/addCart/{id}/{num}")
    public R addCart(@PathVariable String id, @PathVariable Integer num, HttpServletRequest request) {
        return R.ok().put("data", cartService.addCart(id, num, request));
    }

    @GetMapping(value = "/queryCart")
    public R queryCart(HttpServletRequest request) {
        return R.ok().put("data", cartService.queryCart(request));
    }
    @GetMapping("/checkCart/{isChecked}/{id}")
    public R checkCart(@PathVariable Integer isChecked, @PathVariable String id,HttpServletRequest request) {
        return R.ok().put("data",cartService.checkItem(id, isChecked,request));
    }
    @GetMapping("/deleteItem/{id}")
    public R deleteItem(@PathVariable String id,HttpServletRequest request) {
        return R.ok().put("data",cartService.deleteItem(id,request));
    }
}
